package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Camera;
import com.funmi.lizip.entity.EcLog;
import com.funmi.lizip.entity.Photo;
import com.funmi.lizip.entity.dto.EcLog.EcLogAddRequest;
import com.funmi.lizip.entity.dto.EcLog.EcLogListQueryRequest;
import com.funmi.lizip.entity.dto.camera.CameraAddRequest;
import com.funmi.lizip.entity.dto.photo.PhotoQueryRequest;
import com.funmi.lizip.entity.vo.EcLogListVo;
import com.funmi.lizip.entity.vo.EcLogVo;
import com.funmi.lizip.entity.vo.PhotoVo;
import com.funmi.lizip.service.EcLogService;
import com.funmi.lizip.utils.MyJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "巡护日志")
@RestController
@RequestMapping("/ecLog")
public class EcLogController {

    @Autowired
    private EcLogService ecLogService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "列表")
    @GetMapping("/rangerId")
    public R list(EcLogListQueryRequest request){
        QueryWrapper<EcLog> ecLogQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(request) && !StringUtils.isEmpty(request.getRangerId())){
            ecLogQueryWrapper.eq("ranger_id",request.getRangerId());

        }
        ecLogQueryWrapper.orderByDesc("gmt_create");
        long current = 1;
        long size = 10;
        if (request != null){
            current = request.getPage();
            size = request.getPageSize();
        }
        Page<EcLog> ecLogPage = ecLogService.page(new Page<>(current, size), ecLogQueryWrapper);
        Page<EcLogListVo> ecLogVoPage = new PageDTO(ecLogPage.getCurrent(), ecLogPage.getSize(), ecLogPage.getTotal());
        List<EcLogListVo> ecLogVoList = ecLogPage.getRecords().stream().map(ecLog -> {
            EcLogListVo ecLogListVo = new EcLogListVo();
            BeanUtils.copyProperties(ecLog, ecLogListVo);
            return ecLogListVo;
        }).collect(Collectors.toList());
        ecLogVoPage.setRecords(ecLogVoList);
        return R.success().data(ecLogVoPage);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据id获取")
    @GetMapping("/{id}")
    public R id(@PathVariable Integer id) {
        EcLog byId = ecLogService.getById(id);
        EcLogVo ecLogVo = new EcLogVo();
        if(byId != null) {
            BeanUtils.copyProperties(byId, ecLogVo);
            ecLogVo.setGmt_create(byId.getGmtCreate());
        }
        return R.success().data(ecLogVo);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增")
    @PostMapping("")
    public R addInfraredCamera(@RequestBody EcLogAddRequest request){
        EcLog ecLog = new EcLog();
        BeanUtils.copyProperties(request, ecLog);
        boolean save = ecLogService.save(ecLog);
        if (save) {
            return R.success().data("添加成功");
        }
        return R.fail().data("添加日志失败");
    }



}
